var storage, fail, uid;
try {
  uid = new Date();
  (storage = window.localStorage).setItem(uid, uid);
  fail = storage.getItem(uid) != uid;
  storage.removeItem(uid);
  fail && (storage = false);
} catch (e) {}
if (storage) {
  var usedSkin = localStorage.getItem("config-skin");
  if (usedSkin != "" && usedSkin != null) {
    document.body.className = usedSkin;
  } else {
    document.body.className = "theme-blue-gradient";
    localStorage.setItem("config-skin", "theme-blue-gradient");
  }
} else {
  document.body.className = "theme-blue";
}
$(function () {
  if (storage) {
    try {
      var a = localStorage.getItem("config-skin");
      if (a != "") {
        $("#skin-colors .skin-changer").removeClass("active");
        $('#skin-colors .skin-changer[data-skin="' + a + '"]').addClass(
          "active"
        );
      }
    } catch (e) {
      console.log(e);
    }
  }
});
$.fn.removeClassPrefix = function (d) {
  this.each(function (i, a) {
    var b = a.className.split(" ").filter(function (c) {
      return c.lastIndexOf(d, 0) !== 0;
    });
    a.className = b.join(" ");
  });
  return this;
};
$(function ($) {
  $("#config-tool-cog").on("click", function () {
    $("#config-tool").toggleClass("closed");
  });
  $("#config-fixed-header").on("change", function () {
    var a = "";
    if ($(this).is(":checked")) {
      $("body").addClass("fixed-header");
      a = "fixed-header";
    } else {
      $("body").removeClass("fixed-header");
      if ($("#config-fixed-sidebar").is(":checked")) {
        $("#config-fixed-sidebar").prop("checked", false);
        $("#config-fixed-sidebar").trigger("change");
        location.reload();
      }
    }
  });
  $("#skin-colors .skin-changer").on("click", function () {
    $("body").removeClassPrefix("theme-");
    $("body").addClass($(this).data("skin"));
    $("#skin-colors .skin-changer").removeClass("active");
    $(this).addClass("active");
    writeStorage(storage, "config-skin", $(this).data("skin"));
  });
  function writeStorage(a, b, c) {
    if (a) {
      try {
        localStorage.setItem(b, c);
      } catch (e) {
        console.log(e);
      }
    }
  }
});
$(function ($) {
  $("#content-wrapper").find(".mainContent").height($(window).height());
  $(window).resize(function (e) {
    $("#content-wrapper").find(".mainContent").height($(window).height());
  });
  $("#sidebar-nav,#nav-col-submenu").on(
    "click",
    ".dropdown-toggle",
    function (e) {
      e.preventDefault();
      var a = $(this).parent();
      if (!a.hasClass("open")) {
        a.parent().find(".open .submenu").slideUp("fast");
        a.parent().find(".open").toggleClass("open");
      }
      a.toggleClass("open");
      if (a.hasClass("open")) {
        a.children(".submenu").slideDown("fast", function () {});
      } else {
        a.children(".submenu").slideUp("fast");
      }
    }
  );
  $("body").on(
    "mouseenter",
    "#page-wrapper.nav-small #sidebar-nav .dropdown-toggle",
    function (e) {
      if ($(document).width() >= 992) {
        var a = $(this).parent();
        if ($("body").hasClass("fixed-leftmenu")) {
          var b = a.position().top;
          if (b + 4 * $(this).outerHeight() >= $(window).height()) {
            b -= 6 * $(this).outerHeight();
          }
          $("#nav-col-submenu").html(a.children(".submenu").clone());
          $("#nav-col-submenu > .submenu").css({ top: b });
        }
        a.addClass("open");
        a.children(".submenu").slideDown("fast");
      }
    }
  );
  $("body").on(
    "mouseleave",
    "#page-wrapper.nav-small #sidebar-nav > .nav-pills > li",
    function (e) {
      if ($(document).width() >= 992) {
        var a = $(this);
        if (a.hasClass("open")) {
          a.find(".open .submenu").slideUp("fast");
          a.find(".open").removeClass("open");
          a.children(".submenu").slideUp("fast");
        }
        a.removeClass("open");
      }
    }
  );
  $("body").on(
    "mouseenter",
    "#page-wrapper.nav-small #sidebar-nav a:not(.dropdown-toggle)",
    function (e) {
      if ($("body").hasClass("fixed-leftmenu")) {
        $("#nav-col-submenu").html("");
      }
    }
  );
  $("body").on("mouseleave", "#page-wrapper.nav-small #nav-col", function (e) {
    if ($("body").hasClass("fixed-leftmenu")) {
      $("#nav-col-submenu").html("");
    }
  });
  $("body")
    .find("#make-small-nav")
    .click(function (e) {
      $("#page-wrapper").toggleClass("nav-small");
    });
  $("body")
    .find(".mobile-search")
    .click(function (e) {
      e.preventDefault();
      $(".mobile-search").addClass("active");
      $(".mobile-search form input.form-control").focus();
    });
  $(document).mouseup(function (e) {
    var a = $(".mobile-search");
    if (!a.is(e.target) && a.has(e.target).length === 0) {
      a.removeClass("active");
    }
  });
  $(window).load(function () {
    window.setTimeout(function () {
      $("#ajax-loader").fadeOut();
    }, 300);
  });
});
function GetChildTree(c) {
  var d = "";
  var e = c.ChildNodes;
  if (e != null && e.length > 0) {
    d += '<ul class="submenu" >';
    $.each(e, function (i) {
      var a = e[i];
      d += "<li>";
      if (a.ChildNodes != null && a.ChildNodes.length > 0) {
        d +=
          '<a  data-id="' +
          a.F_Id +
          '" href="#" class="dropdown-toggle"><i class="' +
          a.F_Icon +
          '"></i><span>' +
          a.F_FullName +
          '</span><i class="fa fa-angle-right drop-icon"></i></a>';
      } else {
        d +=
          '<a class="submenuitem"  data-id="' +
          a.F_Id +
          '" href="' +
          a.F_UrlAddress +
          '" data-index="' +
          a.F_SortCode +
          '">' +
          a.F_FullName +
          "</a>";
      }
      var b = GetChildTree(a);
      if (b != "") {
        d += b;
      }
      d += "</li>";
    });
    d += "</ul>";
  }
  return d;
}
